<?php

defined('SYSPATH') or die('No direct script access.');

class News {

    private static $instances;
    private $data;

    public static function & instances($id = 0) {

        if (!isset(self::$instances[$id])) {
            $class = __CLASS__;
            self::$instances[$id] = new $class($id);
        }
        return self::$instances[$id];
    }

    public function __construct($id) {

        $this->data = NULL;
        $this->_load($id);
    }

    public function _load($id) {

        if (!$id)
            return FALSE;

        $result = ORM::factory('new')
                ->where('id_news', '=', $id)
                ->find()
                ->as_array();

        $this->data = $result ? $result : array();
    }

    public function get($key = NULL) {

        if (is_null($key)) {
            return $this->data;
        } else {
            return isset($this->data[$key]) ? $this->data[$key] : NULL;
        }
    }

    public function set($data) {

        $news = ORM::factory('new');
        $news->values($data);
        if ($news->check()) {
            $news->save();
            if ($news->saved()) {
                $this->add_news_images($news->id_news);
                return true;
            }
        }

        return false;
    }

    public function add_news_images($id_news) {
        $imagesfile_define = Session::instance()->get('imagesfile_define');
        if (is_array($imagesfile_define)) {
            foreach ($imagesfile_define as $key => $value) {
                $news_image = ORM::factory('news_image');
                $news_image->id_newimage = $id_news;
                $news_image->url_image = $value;
                $news_image->type = 2;
                $news_image->addtime = time();
                if ($news_image->check()) {
                    $news_image->save();
                }
                if (Session::instance()->get('temp_imagefiles')) {
                    Session::instance()->delete('temp_imagefiles');
                }

                if (Session::instance()->get('imagesfile_define')) {
                    Session::instance()->delete('imagesfile_define');
                }
            }
        }
    }

    public function update($data) {
        $news = ORM::factory('new')
                ->where('id_news', '=', $this->data['id_news'])
                ->find();

        if (!$news->loaded()) {
            return false;
        } else {
            $news->values($data);
            if ($news->check()) {
                $news->save();
                if ($news->saved()) {
                    return true;
                }
            }
        }
    }

    public function delete() {
        $news = ORM::factory('new', $this->data['id_news']);
        if ($news->loaded()) {
            return $news->delete();
        } else {
            return FALSE;
        }
    }

    public function get_categories($id = '') {

        $catalog_opt_template = array(
            'name_box' => '<option value="{id}" {selected}>{indentation}{name}</option>',
            'indentation' => '&nbsp;|&nbsp;&nbsp;'
        );
        $news_categories = Catehelp::tree_to_str(Catehelp::get_tree($catalog_opt_template, 0, 0, array('table' => 'news_categories', 'id' => 'id_category', 'name' => 'name'), $id));

        return $news_categories;
    }

}

